package com.wan37.client.service;

import com.wan37.protobuf.message.client2server.game.GameEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.PriorityBlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author : luoyong
 * @date : 2020-06-09 10:13
 **/
public class SkillService {

    private final static Logger logger = LoggerFactory.getLogger(SkillService.class);

    public static boolean stop = false;

    /**
     * 存放当前实体id可以释放技能的map
     */
    public static volatile Map<String, PriorityBlockingQueue<GameEntity.Skill>> allRoleIdToPBQMap = new ConcurrentHashMap<>();

    /**
     * bossId对于拥有的技能
     */
    public static Map<String, Map<String, GameEntity.Skill>> allRoleIdToSkillMap = new ConcurrentHashMap<>();

    /**
     * 技能id对应技能的Map
     */
    public static Map<String, GameEntity.Skill> skillMap = new ConcurrentHashMap<>();


    /**
     * 根据传入的一个实体Id,获取优先级技能队列，从队列中拿到一个技能
     *
     * @param allRoleId
     * @return
     */
    public static GameEntity.Skill getPbqSkill(String allRoleId) {
        try {
            return allRoleIdToPBQMap.get(allRoleId).take();
        } catch (InterruptedException e) {
            logger.info("-----------获取技能请求被中断！！");
        }
        return null;
    }

    /**
     * 通过实体id从实体id对应的优先级别技能队列中获取对应队列，将skill放入队列中
     *
     * @param allRoleId
     * @param skill
     * @return
     */
    public static boolean addPbqSkill(String allRoleId, GameEntity.Skill skill) {
        return allRoleIdToPBQMap.get(allRoleId).offer(skill);
    }

    /**
     * @param allRoleId
     */
    public static void addAllRoleIdToPBQMap(String allRoleId) {
        allRoleIdToPBQMap.put(allRoleId,
                new PriorityBlockingQueue<>(10, (s1, s2) -> s2.getPriority() - s1.getPriority()));
    }


    public static void clearAllRoleIdToPBQMap() {
        allRoleIdToPBQMap.clear();
    }

//    public void execute(){
//        while (!stop){
//            GameEntity.Skill skill = getPbqSkill();
//            if (skill != null){
//
//            }
//        }
//    }


}
